﻿using System;
using System.Collections.Generic;
using Lottery.App.Model;
using Lottery.App.DAL;

namespace Lottery.App
{
    public class GlobalData
    {
        public static int CurrentID { get; private set; }
        public static IList<RecordInfo> Records { get; private set; }

        public static string NextQH 
        {
            get {
                string v = Records[Records.Count - 1].Date;
                int year = Convert.ToInt32(v.Substring(0,4));
                int month = Convert.ToInt32(v.Substring(4,2));
                int day = Convert.ToInt32(v.Substring(6,2));
                
                DateTime dt = new DateTime(year, month, day);
                int num = Records[Records.Count - 1].Num + 1;

                if (num > 84)
                {
                    num = 1;
                    dt = dt.AddDays(1);
                }

                return String.Format("{0:yyyyMMdd}{1:D3}", dt, num); 
            } 
        }
        
        private GlobalData() { }
        static GlobalData() 
        {
            Refresh();
        }

        public static void Refresh()
        {
            Records = new RecordDao().GetAllObjects();
            CurrentID = Records.Count;
        }

        public static void Refresh(RecordInfo record)
        {
            Records.Add(record);
            CurrentID = record.ID;
        }


    }
}
